﻿package app.view.components.guiDima.keyboard 
{
	import app.model.types.LangTypes;
	import app.view.components.buttons.KeyBoardBtnD;
	import app.view.events.KeyboardEventD;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	/**
	 * Базовый класс клавиатура + поле ввода
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class KeyBoardDBase extends Sprite
	{
		public var btn_01:KeyBoardBtnD;
		public var btn_02:KeyBoardBtnD;
		public var btn_03:KeyBoardBtnD;
		public var btn_04:KeyBoardBtnD;
		public var btn_05:KeyBoardBtnD;
		public var btn_06:KeyBoardBtnD;
		public var btn_07:KeyBoardBtnD;
		public var btn_08:KeyBoardBtnD;
		public var btn_09:KeyBoardBtnD;
		public var btn_10:KeyBoardBtnD;
		public var btn_11:KeyBoardBtnD;
		public var btn_12:KeyBoardBtnD;
		public var btn_13:KeyBoardBtnD;
		public var btn_14:KeyBoardBtnD;
		public var btn_15:KeyBoardBtnD;
		public var btn_16:KeyBoardBtnD;
		public var btn_17:KeyBoardBtnD;
		public var btn_18:KeyBoardBtnD;
		public var btn_19:KeyBoardBtnD;
		public var btn_20:KeyBoardBtnD;
		public var btn_21:KeyBoardBtnD;
		public var btn_22:KeyBoardBtnD;
		public var btn_23:KeyBoardBtnD;
		public var btn_24:KeyBoardBtnD;
		public var btn_25:KeyBoardBtnD;
		public var btn_26:KeyBoardBtnD;
		public var btn_27:KeyBoardBtnD;
		public var btn_28:KeyBoardBtnD;
		public var btn_29:KeyBoardBtnD;
		public var btn_30:KeyBoardBtnD;
		public var btn_31:KeyBoardBtnD;
		public var btn_32:KeyBoardBtnD;
		public var btn_33:KeyBoardBtnD;
		public var btn_34:KeyBoardBtnD;
		public var btn_35:KeyBoardBtnD;
		public var btn_36:KeyBoardBtnD;
		public var btn_37:KeyBoardBtnD;
		public var btn_38:KeyBoardBtnD;
		public var btn_39:KeyBoardBtnD;
		public var btn_40:KeyBoardBtnD;
		public var btn_41:KeyBoardBtnD;
		public var btn_42:KeyBoardBtnD;
		public var btn_43:KeyBoardBtnD;
		public var btn_44:KeyBoardBtnD;
		public var btn_45:KeyBoardBtnD;
		public var btn_46:KeyBoardBtnD;
		public var btn_47:KeyBoardBtnD;
		public var btn_48:KeyBoardBtnD;
		public var btn_49:KeyBoardBtnD;
		public var btn_50:KeyBoardBtnD;
		public var btn_51:KeyBoardBtnD;
		public var btn_52:KeyBoardBtnD;
		public var btn_53:KeyBoardBtnD;
		public var btn_54:KeyBoardBtnD;
		public var btn_55:KeyBoardBtnD;
		public var btn_56:KeyBoardBtnD;
		//public var btn_57:KeyBoardBtnD;
		//public var btn_58:KeyBoardBtnD;
		
		private var _items:/*KeyBoardBtnD*/Array;
		private var _dictionary:Dictionary;
		
		
		
		// состояния клавиатуры
		public var boardRusLowCase:MovieClip;
		public var boardRusUpCase:MovieClip;
		public var boardEngLowCase:MovieClip;
		public var boardEngUpCase:MovieClip;
		
		private static const STATE_RUS_UP_CASE:String = 'STATE_RUS_UP_CASE';
		private static const STATE_RUS_LOW_CASE:String = 'STATE_RUS_LOW_CASE';
		private static const STATE_ENG_UP_CASE:String = 'STATE_ENG_UP_CASE';
		private static const STATE_ENG_LOW_CASE:String = 'STATE_ENG_LOW_CASE';
		
		public static const LANG_RUS:String = LangTypes.RUSSIAN;
		public static const LANG_ENG:String = LangTypes.ENGLISH;
		
		
		private var _currentLang:String=LANG_RUS;
		private var _currentCapsLock:Boolean=false;
		
		private static const KEY_BACKSPACE:String = 'key_backspace';
		private static const KEY_ENTER:String = 'key_enter';
		private static const KEY_SPACE:String = 'key_space';
		private static const KEY_CAPSLOCK:String = 'key_capslock';
		private static const KEY_SMILE:String = ':)';
		private static const KEY_LANG:String = 'key_lang';
		private static const KEY_LEFT:String = 'key_left';
		private static const KEY_RIGHT:String = 'key_right';
		
		// для запоминания и сдвига при переключчении языка
		// пробел
		private static const BGD_BACKSPACE_X_RUS:Number = 1154.7;
		private static const BGD_BACKSPACE_Y_RUS:Number = 8.3;
		private static const BGD_BACKSPACE_X_ENG:Number = 976;
		private static const BGD_BACKSPACE_Y_ENG:Number = 8.3;
		// энтер
		private static const BGD_ENTER_X_RUS:Number = 1081.5;
		private static const BGD_ENTER_Y_RUS:Number = 97.8;
		private static const BGD_ENTER_X_ENG:Number = 919.5;
		private static const BGD_ENTER_Y_ENG:Number = 97.8;
		// точка
		private static const BGD_POINT_X_RUS:Number = 965.3;
		private static const BGD_POINT_Y_RUS:Number = 186.2;
		private static const BGD_POINT_X_ENG:Number = 783.3;
		private static const BGD_POINT_Y_ENG:Number = 184.2;
		// запятая
		private static const BGD_POINT_C_X_RUS:Number = 1052.6;
		private static const BGD_POINT_C_Y_RUS:Number = 182.2;
		private static const BGD_POINT_C_X_ENG:Number = 870.5;
		private static const BGD_POINT_C_Y_ENG:Number = 182.2;
		// @
		private static const BGD_DOG_X_RUS:Number = 1148.1;
		private static const BGD_DOG_Y_RUS:Number = 189.5;
		private static const BGD_DOG_X_ENG:Number = 968.1;
		private static const BGD_DOG_Y_ENG:Number = 188.5;
		// :-)
		private static const BGD_SMILE_X_RUS:Number = 1193.5;
		private static const BGD_SMILE_Y_RUS:Number = 267.3;
		private static const BGD_SMILE_X_ENG:Number = 1051.5;
		private static const BGD_SMILE_Y_ENG:Number = 187.3;
		private var _isBlock:Boolean=false;
		
		
		public function KeyBoardDBase() 
		{
			init();
		}
		
		private function init():void
		{
			boardRusLowCase.visible = false;
			boardRusUpCase.visible = false;
			boardEngLowCase.visible = false;
			boardEngUpCase.visible = false;
			
		}
		
		public function setFirstParam(lang:String=LANG_RUS,caps:Boolean=false):void
		{
			createDictionary();
			
			setBtnKeyId();
			
			
			currentLang = lang;
			
			currentCapsLock = caps;
			
			setStateBoard(getStateOnLangAndCaps());
			
			setEvents();
		}
		
		public function blockAllKeyWithoutBackSpace():void
		{
			
			if (_isBlock==false) 
			{
				for (var i:int = 0; i < _items.length; i++) 
				{
					if (_items[i].keyID!=13 && _items[i].keyID!=47 && _items[i].keyID!=49) 
					{
						_items[i].isBlock = true;
						//_items[i].mouseEnabled = false;
					}
				}
				_isBlock = true;
			}
			
		}
		
		public function unBlockAllKey():void
		{
			if (_isBlock==true) 
			{
				for (var i:int = 0; i < _items.length; i++) 
				{
					_items[i].isBlock = false;
				}
				
				_isBlock = false;
			}
			
		}
		
		/**
		 * получения имени состояния по текущим значения языка и капса
		 * @return
		 */
		private function getStateOnLangAndCaps():String
		{
			var st:String;
			
			if (_currentLang==LANG_RUS) 
			{
				_currentCapsLock == false ? st = STATE_RUS_LOW_CASE : st = STATE_RUS_UP_CASE;
			}
			
			if (_currentLang==LANG_ENG) 
			{
				_currentCapsLock == false ? st = STATE_ENG_LOW_CASE : st = STATE_ENG_UP_CASE;
			}
			
			return st;
		}
		
		private function setEvents():void
		{
			addEventListener(KeyboardEventD.ON_KEY_DOWN, onKeyDown, true);
		}
		
		private function removeEvents():void
		{
			removeEventListener(KeyboardEventD.ON_KEY_DOWN, onKeyDown, true);
			for (var i:int = 0; i < _items.length; i++) 
			{
				_items[i].destroy();
			}
		}
		
		/**
		 * выставить состояние клавы
		 */
		private function setStateBoard(stateBoard:String=STATE_RUS_LOW_CASE):void
		{
			switch (stateBoard) 
			{
				case STATE_RUS_LOW_CASE:
					boardRusLowCase.visible = true;
					boardRusUpCase.visible = false;
					boardEngLowCase.visible = false;
					boardEngUpCase.visible = false;
				break;
							
				case STATE_RUS_UP_CASE:
					boardRusLowCase.visible = false;
					boardRusUpCase.visible = true;
					boardEngLowCase.visible = false;
					boardEngUpCase.visible = false;	
				break;
				
				case STATE_ENG_LOW_CASE:
					boardRusLowCase.visible = false;
					boardRusUpCase.visible = false;
					boardEngLowCase.visible = true;
					boardEngUpCase.visible = false;	
				break;
				
				case STATE_ENG_UP_CASE:
					boardRusLowCase.visible = false;
					boardRusUpCase.visible = false;
					boardEngLowCase.visible = false;
					boardEngUpCase.visible = true;	
				break;
			}
			
			
		}
		
		
		/**
		 * создадим словарь по кнопкам
		 */
		private function createDictionary():void
		{
			_dictionary = new Dictionary();
			_dictionary[1] = 	{ rus:'й', 				eng:'q', 				china:'' }
			_dictionary[2] = 	{ rus:'ц', 				eng:'w', 				china:'' }
			_dictionary[3] = 	{ rus:'у', 				eng:'e', 				china:'' }
			_dictionary[4] = 	{ rus:'к', 				eng:'r', 				china:'' }
			_dictionary[5] = 	{ rus:'е', 				eng:'t', 				china:'' }
			_dictionary[6] = 	{ rus:'н', 				eng:'y', 				china:'' }
			_dictionary[7] = 	{ rus:'г', 				eng:'u', 				china:'' }
			_dictionary[8] = 	{ rus:'ш', 				eng:'i', 				china:'' }
			_dictionary[9] = 	{ rus:'щ', 				eng:'o', 				china:'' }
			_dictionary[10] = 	{ rus:'з', 				eng:'p', 				china:'' }
			_dictionary[11] = 	{ rus:'х', 				eng:'', 				china:'' }
			_dictionary[12] = 	{ rus:'ъ', 				eng:'', 				china:'' }
			_dictionary[13] = 	{ rus:KEY_BACKSPACE, 	eng:KEY_BACKSPACE, 		china:KEY_BACKSPACE }
			_dictionary[14] = 	{ rus:'1', 				eng:'1', 				china:'1' }
			_dictionary[15] = 	{ rus:'2', 				eng:'2', 				china:'2' }
			_dictionary[16] = 	{ rus:'3', 				eng:'3', 				china:'3' }
			
			_dictionary[17] = 	{ rus:'ф', 				eng:'a', 				china:'' }
			_dictionary[18] = 	{ rus:'ы', 				eng:'s', 				china:'' }
			_dictionary[19] = 	{ rus:'в', 				eng:'d', 				china:'' }
			_dictionary[20] = 	{ rus:'а', 				eng:'f', 				china:'' }
			_dictionary[21] = 	{ rus:'п', 				eng:'g', 				china:'' }
			_dictionary[22] = 	{ rus:'р', 				eng:'h', 				china:'' }
			_dictionary[23] = 	{ rus:'о', 				eng:'j', 				china:'' }
			_dictionary[24] = 	{ rus:'л', 				eng:'k', 				china:'' }
			_dictionary[25] = 	{ rus:'д', 				eng:'l', 				china:'' }
			_dictionary[26] = 	{ rus:'ж', 				eng:'', 				china:'' }
			_dictionary[27] = 	{ rus:'э', 				eng:'', 				china:'' }
			
			_dictionary[28] = 	{ rus:KEY_ENTER, 		eng:KEY_ENTER, 			china:KEY_ENTER }
			
			_dictionary[29] = 	{ rus:'4', 				eng:'4', 				china:'4' }
			_dictionary[30] = 	{ rus:'5', 				eng:'5', 				china:'5' }
			_dictionary[31] = 	{ rus:'6', 				eng:'6', 				china:'6' }
			
			_dictionary[32] = 	{ rus:'я', 				eng:'z', 				china:'' }
			_dictionary[33] = 	{ rus:'ч', 				eng:'x', 				china:'' }
			_dictionary[34] = 	{ rus:'с', 				eng:'c', 				china:'' }
			_dictionary[35] = 	{ rus:'м', 				eng:'v', 				china:'' }
			_dictionary[36] = 	{ rus:'и', 				eng:'b', 				china:'' }
			_dictionary[37] = 	{ rus:'т', 				eng:'n', 				china:'' }
			_dictionary[38] = 	{ rus:'ь', 				eng:'m', 				china:'' }
			_dictionary[39] = 	{ rus:'б', 				eng:'', 				china:'' }
			_dictionary[40] = 	{ rus:'ю', 				eng:'', 				china:'' }
			_dictionary[41] = 	{ rus:'.', 				eng:'.', 				china:'.' }
			_dictionary[42] = 	{ rus:',', 				eng:',', 				china:',' }
			_dictionary[43] = 	{ rus:'@', 				eng:'@',	 			china:'@' }
			
			_dictionary[44] = 	{ rus:'7', 				eng:'7', 				china:'7' }
			_dictionary[45] = 	{ rus:'8', 				eng:'8', 				china:'8' }
			_dictionary[46] = 	{ rus:'9', 				eng:'9', 				china:'9' }
			
			_dictionary[47] = 	{ rus:KEY_CAPSLOCK, 	eng:KEY_CAPSLOCK, 		china:KEY_CAPSLOCK }
			_dictionary[48] = 	{ rus:' ', 				eng:' ', 				china:' ' }
			_dictionary[49] = 	{ rus:KEY_LANG, 		eng:KEY_LANG, 			china:KEY_LANG }
			_dictionary[50] = 	{ rus:'!', 				eng:'!', 				china:'!' }
			_dictionary[51] = 	{ rus:'?', 				eng:'?', 				china:'?' }
			_dictionary[52] = 	{ rus:':', 				eng:':', 				china:':' }
			_dictionary[53] = 	{ rus:'-', 				eng:'-', 				china:'-' }
			_dictionary[54] = 	{ rus:'_', 				eng:'_', 				china:'_' }
			_dictionary[55] = 	{ rus:KEY_SMILE, 		eng:KEY_SMILE, 			china:KEY_SMILE }
			_dictionary[56] = 	{ rus:'0', 				eng:'0', 				china:'0' }
			//_dictionary[57] = 	{ rus:KEY_LEFT, 		eng:KEY_LEFT, 			china:KEY_LEFT }
			//_dictionary[58] = 	{ rus:KEY_RIGHT, 		eng:KEY_RIGHT, 			china:KEY_RIGHT }
			
			
			//tf.text = String.fromCharCode(e.keyCode)
			////trace(e.keyCode, String.fromCharCode(e.keyCode), d[49].eng);
		}
		
		public function destroy():void
		{
			removeEvents();
		}
		
		/**
		 * установка айдишников кнопкам
		 * считал их слева-направо, сверху вниз, начиная с "1"
		 */
		private function setBtnKeyId():void
		{
			// вначале свалим в масси потом проставим айдишники в цикле
			_items = new Array();
			_items.push(btn_01);
			_items.push(btn_02);
			_items.push(btn_03);
			_items.push(btn_04);
			_items.push(btn_05);
			_items.push(btn_06);
			_items.push(btn_07);
			_items.push(btn_08);
			_items.push(btn_09);
			_items.push(btn_10);
			_items.push(btn_11);
			_items.push(btn_12);
			_items.push(btn_13);
			_items.push(btn_14);
			_items.push(btn_15);
			_items.push(btn_16);
			_items.push(btn_17);
			_items.push(btn_18);
			_items.push(btn_19);
			_items.push(btn_20);
			_items.push(btn_21);
			_items.push(btn_22);
			_items.push(btn_23);
			_items.push(btn_24);
			_items.push(btn_25);
			_items.push(btn_26);
			_items.push(btn_27);
			_items.push(btn_28);
			_items.push(btn_29);
			_items.push(btn_30);
			_items.push(btn_31);
			_items.push(btn_32);
			_items.push(btn_33);
			_items.push(btn_34);
			_items.push(btn_35);
			_items.push(btn_36);
			_items.push(btn_37);
			_items.push(btn_38);
			_items.push(btn_39);
			_items.push(btn_40);
			_items.push(btn_41);
			_items.push(btn_42);
			_items.push(btn_43);
			_items.push(btn_44);
			_items.push(btn_45);
			_items.push(btn_46);
			_items.push(btn_47);
			_items.push(btn_48);
			_items.push(btn_49);
			_items.push(btn_50);
			_items.push(btn_51);
			_items.push(btn_52);
			_items.push(btn_53);
			_items.push(btn_54);
			_items.push(btn_55);
			_items.push(btn_56);
			//_items.push(btn_57);
			//_items.push(btn_58);
			
			// проставим айдишники кнопкам
			for (var i:int = 0; i < _items.length; i++) 
			{
				_items[i].keyID = i + 1;
			}
		}
		
		/**
		 * нажали кнопку на клаве
		 * @param	e
		 */
		private function onKeyDown(e:KeyboardEventD):void 
		{
			////trace('id key', e.id);
			//trace(_dictionary[e.id].rus);
			
			// сбрасываем выделение
			for (var i:int = 0; i < _items.length; i++) 
			{
				if (_items[i].keyID!=e.id) 
				{
					_items[i].onMouseUp(null);
				}
			}
			
			if (_dictionary[e.id].rus==KEY_CAPSLOCK || _dictionary[e.id].rus==KEY_LANG) 
			{
				if (_dictionary[e.id].rus==KEY_CAPSLOCK) 
				{
					_currentCapsLock = !_currentCapsLock;
					
					setStateBoard(getStateOnLangAndCaps());
				}
				if (_dictionary[e.id].rus==KEY_LANG) 
				{
					var b:String;
					
					if (_currentLang==LANG_RUS) 
					{
						b = LANG_ENG;
					}
					if (_currentLang==LANG_ENG) 
					{
						b = LANG_RUS;
					}
					currentLang = b;
					
					setStateBoard(getStateOnLangAndCaps());
				}
				
			}else
			{
				// вводим текст
				var str:String;
				var ev:KeyboardEventD;
				
				if (_currentLang==LANG_RUS) 
				{
					////trace(_dictionary[e.id].rus);
					
					if (_dictionary[e.id].rus!=KEY_BACKSPACE && _dictionary[e.id].rus!=KEY_ENTER && _dictionary[e.id].rus!=KEY_LEFT && _dictionary[e.id].rus!=KEY_RIGHT) 
					{
						str = _dictionary[e.id].rus;
						
						_currentCapsLock == true ? str = str.toUpperCase() : 0;
						
						ev = new KeyboardEventD(KeyboardEventD.ON_INPUT_TEXT);
						ev.str = str;
						
						
					}else
					{
						if (_dictionary[e.id].rus==KEY_BACKSPACE) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_BACKSPACE);
						}
						
						if (_dictionary[e.id].rus==KEY_ENTER) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_ENTER);
						}
						
						if (_dictionary[e.id].rus==KEY_LEFT) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_LEFT);
						}
						
						if (_dictionary[e.id].rus==KEY_RIGHT) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_RIGHT);
						}
						
					}
					
					dispatchEvent(ev);
					
				}
				
				if (_currentLang==LANG_ENG) 
				{
					////trace(_dictionary[e.id].eng);
					if (_dictionary[e.id].eng!=KEY_BACKSPACE && _dictionary[e.id].eng!=KEY_ENTER && _dictionary[e.id].rus!=KEY_LEFT && _dictionary[e.id].rus!=KEY_RIGHT) 
					{
						str = _dictionary[e.id].eng;
						
						_currentCapsLock == true ? str = str.toUpperCase() : 0;
						
						ev = new KeyboardEventD(KeyboardEventD.ON_INPUT_TEXT);
						ev.str = str;
						
						
					}else
					{
						if (_dictionary[e.id].eng==KEY_BACKSPACE) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_BACKSPACE);
						}
						
						if (_dictionary[e.id].eng==KEY_ENTER) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_ENTER);
						}
						if (_dictionary[e.id].eng==KEY_LEFT) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_LEFT);
						}
						if (_dictionary[e.id].eng==KEY_RIGHT) 
						{
							ev = new KeyboardEventD(KeyboardEventD.ON_KEY_RIGHT);
						}
					}
					
					dispatchEvent(ev);
				}
				
				
			}
			
		}
		
		public function get currentLang():String { return _currentLang; }
		
		public function set currentLang(value:String):void 
		{
			_currentLang = value;
			
			if (_currentLang==LANG_RUS) 
			{
				_items[10].visible = true;
				_items[11].visible = true;
				_items[25].visible = true;
				_items[26].visible = true;
				_items[38].visible = true;
				_items[39].visible = true;
				// сдвиг клавишь
				_items[12].x = BGD_BACKSPACE_X_RUS;
				_items[12].y = BGD_BACKSPACE_Y_RUS;
				
				_items[27].x = BGD_ENTER_X_RUS;
				_items[27].y = BGD_ENTER_Y_RUS;
				
				_items[40].x = BGD_POINT_X_RUS;
				_items[40].y = BGD_POINT_Y_RUS;
				
				_items[41].x = BGD_POINT_C_X_RUS;
				_items[41].y = BGD_POINT_C_Y_RUS;
				
				_items[42].x = BGD_DOG_X_RUS;
				_items[42].y = BGD_DOG_Y_RUS;
				
				_items[54].x = BGD_SMILE_X_RUS;
				_items[54].y = BGD_SMILE_Y_RUS;
				
				
			}
			
			if (_currentLang==LANG_ENG) 
			{
				_items[10].visible = false;
				_items[11].visible = false;
				_items[25].visible = false;
				_items[26].visible = false;
				_items[38].visible = false;
				_items[39].visible = false;
				
				// сдвиг клавишь
				_items[12].x = BGD_BACKSPACE_X_ENG;
				_items[12].y = BGD_BACKSPACE_Y_ENG;
				
				_items[27].x = BGD_ENTER_X_ENG;
				_items[27].y = BGD_ENTER_Y_ENG;
				
				_items[40].x = BGD_POINT_X_ENG;
				_items[40].y = BGD_POINT_Y_ENG;
				
				_items[41].x = BGD_POINT_C_X_ENG;
				_items[41].y = BGD_POINT_C_Y_ENG;
				
				_items[42].x = BGD_DOG_X_ENG;
				_items[42].y = BGD_DOG_Y_ENG;
				
				_items[54].x = BGD_SMILE_X_ENG;
				_items[54].y = BGD_SMILE_Y_ENG;
			}
			
			var ev:KeyboardEventD = new KeyboardEventD(KeyboardEventD.ON_CHANGE_BOARD_LANG);
			dispatchEvent(ev);
		}
		
		public function get currentCapsLock():Boolean { return _currentCapsLock; }
		
		public function set currentCapsLock(value:Boolean):void 
		{
			_currentCapsLock = value;
		}
		
		
	}

}